﻿using System;
using CSUtils;
using G02;
using GlobalTripleJump;
using UnityEngine;

namespace TripleJump {
    /// <summary>
    /// 最佳距离显示
    /// </summary>
    public class CMaxDistanceTips {
        public Transform root;
        Vector3 centerPos;
        float radius;
        float z周长;

        public CMaxDistanceTips Init() {
            var gg = C128_AssetsCore.GetModel(GlobalTripleJumpCfg.artPath_maxDistanceTips, 1);
            gg.SetActive(false);
            this.root = gg.transform;
            return this;
        }

        public void SetData(Vector3 centerPos, float radius) {
            this.centerPos = new Vector3(0, centerPos.y, centerPos.z);
            this.radius = radius; 
            z周长 = 2 * Mathf.PI * radius;
        }

        int roloId;

        public void IsShow(int roloId, bool isShow) {
            this.roloId = roloId;
            root.gameObject.SetActive(isShow);
            isNewRecord = false;
        }

        bool isNewRecord;

        public void SetShow(Vector3 point, Vector3 normal, float currentDistance, float maxDistance) {
            //Debug.LogWarningFormat("C_MaxDistanceTips_______{0}___{1}____{2}___{3}", point, normal, currentDistance, maxDistance);
            // maxDistance = currentDistance;
            float distance0 = maxDistance % z周长 - currentDistance;
            float angle = (distance0 / z周长) * 360;
            if (isNewRecord == false && angle < 0) {
                isNewRecord = true;
                if (CPara.onShowNewRecordTips != null) {
                    CPara.onShowNewRecordTips(roloId, currentDistance);
                }
            }

            point = new Vector3(0, point.y, point.z);
            Vector3 vv = Quaternion.Euler(angle, 0, 0) * (point - centerPos);
            //Debug.DrawLine(vv * 2 + centerPosi, vv * 2 + centerPosi - vv* radius * 3, Color.red, 1000);
            RaycastHit hit;
            if (Physics.Raycast(vv * 2 + centerPos, -vv, out hit, radius * 3)) {
                root.position = hit.point + hit.normal * 0.2f;
                root.rotation = Quaternion.FromToRotation(Vector3.forward, new Vector3(hit.normal.x, -hit.normal.z, hit.normal.y));
                //Debug.LogWarningFormat("MaxDistan______{0}___{1}____{2}_", distance0, angle, root.position);
            } else {
                //Debug.LogErrorFormat("MaxDistan_ss_____{0}___{1}____{2}_", distance0, angle, root.position);
            }
        }

        public class CPara {
            public static Action<int, float> onShowNewRecordTips;
        }
    }
}
